from rest_framework.views import APIView
from utils.config import Config
from utils.result import R
from utils.aliverify_code import * 
from api.models import ItEduThumbLesson,ItEduLessonVideo,ItEduUser
from utils.score import GiveScoreSys


class ThumbAPIView(APIView):

    """
    用户点赞 点踩操作
    """
    def post(self,request,*args, **kwargs):

        video_id = request.data.get("video_id")
        user_pk = request.session.get("user_pk")

        video = ItEduLessonVideo.objects.get(pk=video_id)
        user = ItEduUser.objects.get(pk=user_pk)

        operate = request.data.get("operate")

        if operate > 0:
            video.score = GiveScoreSys.calculate_add_score(video.score,Config.THUMB_UP_SCORE,user.user_info.credit)
        else:
            video.score = GiveScoreSys.calculate_decrease_score(video.score,Config.THUMB_UP_SCORE,user.user_info.credit)

        video.save()

        _ = ItEduThumbLesson.objects.create(
            user = user,
            lesson = video,
            operate = operate
        )

        return R.ok().to_json_response()
